package drds.server.sqlparser.sqlobject.statement.query;

import drds.server.sqlparser.sqlobject.AbstractSqlObject;
import drds.server.sqlparser.sqlobject.expression.Expression;
import drds.server.sqlparser.visitor.Visitor;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class SelectItem extends AbstractSqlObject {

	protected Expression expression;
	protected String alias;

	public SelectItem() {

	}

	public SelectItem(Expression expression) {
		this(expression, null);
	}

	public SelectItem(Expression expression, String alias) {
		this.expression = expression;
		this.alias = alias;

		if (expression != null) {
			expression.setParent(this);
		}
	}

	public void setExpression(Expression expression) {
		this.expression = expression;
		if (expression != null) {
			expression.setParent(this);
		}
	}

	public Expression getExpression() {
		return this.expression;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getAlias() {
		return this.alias;
	}

	public void output(StringBuffer sb) {
		this.expression.output(sb);
		if ((this.alias != null) && (this.alias.length() != 0)) {
			sb.append(" as ");
			sb.append(this.alias);
		}
	}

	protected void accept0(Visitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.expression);
		}
		visitor.endVisit(this);
	}

}
